package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbMaterialTransportDo;
import com.yxx.business.entity.TbMaterialTransportVo;
import com.yxx.business.entity.TbMaterialTransportQueryDto;
import com.yxx.business.entity.TbMaterialTransportEditDto;
import com.yxx.business.service.ITbMaterialTransportService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 材料运输信息Controller
 *
 * @author yxx
 * @date 2025-08-16
 */
@Tag(name = "材料运输信息管理-TbMaterialTransport")
@RestController
@RequestMapping("/business/masterialTransport")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbMaterialTransportController extends BaseControllerPlus {

    private final ITbMaterialTransportService tbMaterialTransportService;

    @Operation(summary = "查询--材料运输信息列表")
    @GetMapping("/list")
    public PageResult<TbMaterialTransportVo> list(@ParameterObject TbMaterialTransportQueryDto tbMaterialTransportQueryDto) {
        return tbMaterialTransportService.selectTbMaterialTransportVoPage(tbMaterialTransportQueryDto);
    }

    @Operation(summary = "查询--材料运输信息单个")
    @GetMapping(value = "/{materialId}")
    public R<TbMaterialTransportVo> getInfo(@PathVariable Long materialId) {
        return R.ok(tbMaterialTransportService.selectTbMaterialTransportVoByMaterialId(materialId));
    }

    @Operation(summary = "新增--材料运输信息")
    @Log(title = "材料运输信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody @Validated TbMaterialTransportEditDto tbMaterialTransportQueryDto) {
        return toResult(tbMaterialTransportService.insertTbMaterialTransport(tbMaterialTransportQueryDto));
    }

    @Operation(summary = "修改--材料运输信息")
    @Log(title = "材料运输信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{materialId}")
    public R<String> update(@PathVariable Long materialId, @RequestBody @Validated TbMaterialTransportEditDto tbMaterialTransportQueryDto) {
        return toResult(tbMaterialTransportService.updateTbMaterialTransport(materialId, tbMaterialTransportQueryDto));
    }

    @Operation(summary = "删除--材料运输信息")
    @Log(title = "材料运输信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialIds}")
    public R<String> delete(@PathVariable List<Long> materialIds) {
        return toResult(tbMaterialTransportService.deleteTbMaterialTransportByMaterialIds(materialIds));
    }

    @Operation(summary = "导出--材料运输信息列表")
    @Log(title = "材料运输信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbMaterialTransportQueryDto tbMaterialTransportQueryDto) {
        List<TbMaterialTransportDo> list = tbMaterialTransportService.selectTbMaterialTransportDoList(tbMaterialTransportQueryDto);
        ExcelUtil<TbMaterialTransportDo> util = new ExcelUtil<>(TbMaterialTransportDo.class);
        util.exportExcel(response, list, "材料运输信息数据");
    }

    @Operation(summary = "导出--材料运输信息模板")
    @PreAuthorize("@ss.hasPermi('business:masterialTransport:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbMaterialTransportDo> util = new ExcelUtil<>(TbMaterialTransportDo.class);
        util.importTemplateExcel(response, "导入材料运输信息模板数据");
    }

    @Operation(summary = "导入--材料运输信息列表")
    @PreAuthorize("@ss.hasPermi('business:masterialTransport:import')")
    @Log(title = "材料运输信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbMaterialTransportDo> util = new ExcelUtil<>(TbMaterialTransportDo.class);
        List<TbMaterialTransportDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbMaterialTransportService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
